/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.hnjz.sys.role;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnjz.common.AppException;
import com.hnjz.common.FyWebResult;
import com.hnjz.common.JsonResultUtil;
import com.hnjz.common.domain.Combobox;
import com.hnjz.common.util.LogUtil;
import com.hnjz.sys.log.LoggerManager;

/**
 * 角色管理的Controller
 * 
 * @author lijingfen
 * @version $Id: RoleController.java,$
 */
@Controller
public class RoleController {

    /**日志*/
    private static final Log log = LogFactory.getLog(RoleController.class);

    @Autowired
    private RoleManager      roleManager;
    @Autowired
    private LoggerManager loggerManager;

    /**
     * 到角色管理的初始界面
     * 
     * @param model {@link ModelMap}
     * @return 角色管理的初始界面
     */
    @RequestMapping(value = "/roleList.htm")
    public String roleList(ModelMap model) {
        return "sys/role/roleList";
    }

    /**
     * 用户管理的查询
     * 
     * @param model {@link ModelMap}
     * @param name 搜索条件，可以按名称，备注搜索
     * @param page 第几页
     * @param pageSize 每页显示多少条
     */
    @RequestMapping(value = "/roleQuery.json", produces = "application/json")
    public void roleQuery(ModelMap model, String name, String page, String pageSize) {
        try {
            page = StringUtils.defaultIfBlank(page, "1");
            FyWebResult re = roleManager.queryRole(name, page, pageSize);
            JsonResultUtil.fyWeb(model, re);
        } catch (Exception e) {
            log.error("查询出错：", e);
        }
    }

    /**
     * 删除一个角色
     * 
     * @param model  {@link ModelMap}
     * @param id 角色id
     */
    @RequestMapping(value = "/delRole.json", produces = "application/json")
    public void delRole(ModelMap model, String id) {
        try {
            roleManager.removeRole(id);
            JsonResultUtil.suncess(model, "删除成功！");
        } catch (Exception e) {
            log.error("删除角色信息错误：", e);
            JsonResultUtil.fail(model, "删除失败！");
        }
    }

    /**
     * 保存一个角色信息
     * 
     * @param model {@link ModelMap}
     * @param funForm  角色管理的form表单
     * @return 角色管理的初始界面
     */
    @RequestMapping(value = "/saveRole.json", produces = "application/json")
    public void saveRole(ModelMap model, RoleForm frm) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("表单信息:" + LogUtil.m(frm));
            }
            roleManager.saveRole(frm);
            JsonResultUtil.suncess(model, "保存成功！");
        } catch (AppException e) {
            log.error("保存信息错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        }
    }

    /**
     * 编辑一个角色信息
     * 
     * @param model {@link ModelMap}
     * @param frm {@link RoleForm},角色的表单对象，默认会在ModelMap里边放一个首字母小写的 roleForm
     * @return 角色管理的初始界面
     */
    @RequestMapping(value = "/editRole.htm")
    public String editRole(ModelMap model, RoleForm frm) {
        if (log.isDebugEnabled()) {
            log.debug("id:" + frm.getId());
        }
        if (StringUtils.isBlank(frm.getId())) {
            return "sys/role/editRole";
        }
        Role po = this.roleManager.getRole(frm.getId());
        frm.setId(po.getId());
        frm.setCode(po.getCode());
        frm.setJsname(po.getJsname());
        frm.setDescription(po.getDescription());
        frm.setIsActive(po.getIsActive());
        return "sys/role/editRole";
    }

    /**
     * 角色下拉列表的公共查询
     */
    @RequestMapping(value = "/queryAllRole.json", produces = "application/json")
    @ResponseBody
    public List<Combobox> queryAllRole() {
        return roleManager.queryAll();
    }

}
